//
//  YBSignInPeopleInfoListViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBSignInPeopleInfoListViewController.h"
#import "YBSignInPeopleInfoListTableViewCell.h"
#define CELLID @"YBSignInPeopleInfoListTableViewCell"
#import "ZJWebViewController.h"

@interface YBSignInPeopleInfoListViewController ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic , assign) BOOL isDelete;

@property (nonatomic , strong) UIButton * rightBtn;

@property (nonatomic , strong) UIView * deleteView;

@property (nonatomic , strong) UIButton * ensureDeleteButton;

@property (nonatomic , strong) UIButton * allSelectButton;

@property (nonatomic , strong) UIButton * addButton;

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) NSMutableArray<YBSignInPeopleInfoModel *> * dataArray;

@property (nonatomic , strong) NSMutableArray<YBSignInPeopleInfoModel *> * currentDeleteArray;
@end

@implementation YBSignInPeopleInfoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isDelete = NO;
    [self judgeIsShowAddButton];
    // Do any additional setup after loading the view.
    self.titlelabel.text = @"报名人员信息";
    [self ybInitUserInterface];
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self ybInitDataSource];
}


-(void)judgeIsShowAddButton{
    NSDate * endDate = [NSDateFormatter dateTimeWithString:self.model.END_TIME];
    if ([endDate timeIntervalSinceDate:[NSDate date]] > 0) {
        self.isShowAddButton = YES;
    }
    else{
        self.isShowAddButton = NO;
    }
}


-(void)ybInitUserInterface{
    self.rightBtn = [ToolClass createUpDownButtonWithFrame:CGRectMake([UIScreen zj_screenWith] - 48, LP_StatusBarHeight, 44, 44) text:@"删除" textColor:[UIColor whiteColor] imageName:@"nav_del" font:13 imageSize:20 theInterval:2];
    [self.rightBtn addTarget:self action:@selector(option_rightBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigView addSubview:self.rightBtn];
    
    [self.view addSubview:self.deleteView];
    [self.deleteView addSubview:self.ensureDeleteButton];
    [self.deleteView addSubview:self.allSelectButton];
    if (self.isShowAddButton) {
        [self.view addSubview:self.addButton];
    }
    [self.view addSubview:self.tableView];
    [self isShowDeleteView];
}


-(void)ybInitDataSource{
    [YBRequestManager request_signInMeetingPeopleListWithHyId:self.model.ID successBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            self.dataArray = [[NSArray yy_modelArrayWithClass:[YBSignInPeopleInfoModel class] json:object[@"RECEIPTS"]] mutableCopy];
            [self.tableView reloadData];
        }
        else{
            [YJProgressHUD showSuccess:@"加载失败" inview:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}


-(void)option_rightBtn:(UIButton *)sender{
    self.isDelete = !self.isDelete;
    if (self.isDelete) {
        [ToolClass changeButtonWithButton:self.rightBtn imageString:@"nav_cancel" titleColor:[UIColor whiteColor] title:@"取消"];
    }
    else{
        [ToolClass changeButtonWithButton:self.rightBtn imageString:@"nav_del" titleColor:[UIColor whiteColor] title:@"删除"];
        for (YBSignInPeopleInfoModel * model in self.currentDeleteArray) {
            model.isSelected = NO;
        }
        [self.currentDeleteArray removeAllObjects];
        self.allSelectButton.selected = NO;
        [self.ensureDeleteButton setTitle:[NSString stringWithFormat:@"确认删除 (%ld)" , self.currentDeleteArray.count] forState:UIControlStateNormal];
    }
    [self isShowDeleteView];
    [self.tableView reloadData];
}

-(void)isShowDeleteView{
    if (self.isDelete) {
        self.deleteView.hidden = NO;
        self.addButton.hidden = YES;
        self.tableView.frame = CGRectMake(0, navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight - ItemHeight * 100);
    }
    else{
        self.deleteView.hidden = YES;
        if (self.isShowAddButton) {
            self.addButton.hidden = NO;
            self.tableView.frame = CGRectMake(0, navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight - ItemHeight * 100);
        }
        else{
            self.tableView.frame = CGRectMake(0, navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight);
        }
        
    }
}

-(void)option_ensureDeleteButton:(UIButton *)sender{
    if (self.currentDeleteArray.count == 0) {
        [YJProgressHUD showSuccess:@"请选择删除对象" inview:self.view];
        return;
    }
    [YJProgressHUD showMessage:@"删除中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    NSMutableArray * myTempArray = [NSMutableArray array];
    for (YBSignInPeopleInfoModel * model in self.currentDeleteArray) {
        [myTempArray addObject:@{@"RECEIPT_ID":[ToolClass returnNoNullStringWithString:model.ID]}];
    }
    [YBRequestManager request_deleteSignInMeetingPeopleListWithPeopleArray:[myTempArray copy] actId:self.model.ID successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [self.currentDeleteArray removeAllObjects];
            [self.ensureDeleteButton setTitle:[NSString stringWithFormat:@"确认删除 (%ld)" , self.currentDeleteArray.count] forState:UIControlStateNormal];
            self.allSelectButton.selected = NO;
            [self ybInitDataSource];
        }
        else{
            [YJProgressHUD hide];
            [YJProgressHUD showSuccess:@"删除失败" inview:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}


-(void)option_allSelectButton:(UIButton *)sender{
    if (self.dataArray.count > 0) {
        sender.selected = !sender.selected;
        [self.currentDeleteArray removeAllObjects];
        for (YBSignInPeopleInfoModel * model in self.dataArray) {
            if (sender.selected) {
                model.isSelected = YES;
                [self.currentDeleteArray addObject:model];
            }
            else{
                model.isSelected = NO;
            }
        }
        [self.tableView reloadData];
        [self.ensureDeleteButton setTitle:[NSString stringWithFormat:@"确认删除 (%ld)" , self.currentDeleteArray.count] forState:UIControlStateNormal];
    }
}


-(void)option_addButton:(UIButton *)sender{
    ZJWebViewController *vc = [ZJWebViewController new];
    NSString * unitId = @"";
    NSString * unitName = @"";
    if (ZJUSERINFORMODEL.DEPT_MODEL.count > 0) {
        unitId = [ToolClass returnNoNullStringWithString:ZJUSERINFORMODEL.DEPT_MODEL[0].ID];
        unitName = [ToolClass returnNoNullStringWithString:ZJUSERINFORMODEL.DEPT_MODEL[0].F_DEPT_NAME];
    }
    NSString *urlString = [NSString stringWithFormat:@"%@/miit/view/writeHZD.html?ACTID=%@&ACCOUNT=%@&UNITNAME=%@",HTMLURL,self.model.ID , ZJUSERINFORMODEL.ORG_ACCOUNT.ID , unitName];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    vc.titleString = @"报名";
    vc.urlString = urlString;
    [self.navigationController pushViewController:vc animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ItemHeight * 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBSignInPeopleInfoListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.selectImageView.hidden = !self.isDelete;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBSignInPeopleInfoModel * tempModel = self.dataArray[indexPath.row];
    if (self.isDelete) {
        tempModel.isSelected = !tempModel.isSelected;
        if (tempModel.isSelected) {
            [self.currentDeleteArray addObject:tempModel];
        }
        else{
            [self.currentDeleteArray removeObject:tempModel];
        }
        [self.tableView reloadData];
        [self judgeIsAllSelected];
    }
    else{
        ZJWebViewController *vc = [ZJWebViewController new];
        NSString *urlString = [NSString stringWithFormat:@"%@/miit/view/receiptDetail.html?actId=%@&&receiptId=%@",HTMLURL,self.model.ID , tempModel.ID];
//        urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        vc.titleString = tempModel.UNIT;
        vc.urlString = urlString;
        [self.navigationController pushViewController:vc animated:YES];
    }
}



-(void)judgeIsAllSelected{
    if (self.currentDeleteArray.count == self.dataArray.count) {
        self.allSelectButton.selected = YES;
    }
    else{
        self.allSelectButton.selected = NO;
    }
    [self.ensureDeleteButton setTitle:[NSString stringWithFormat:@"确认删除 (%ld)" , self.currentDeleteArray.count] forState:UIControlStateNormal];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}



-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - navHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[YBSignInPeopleInfoListTableViewCell class] forCellReuseIdentifier:CELLID];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}


-(UIView *)deleteView
{
    if (!_deleteView) {
        _deleteView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - ItemHeight * 100, [UIScreen zj_screenWith], ItemHeight * 100)];
        _deleteView.backgroundColor = [UIColor whiteColor];
    }
    return _deleteView;
}


-(UIButton *)ensureDeleteButton
{
    if (!_ensureDeleteButton) {
        _ensureDeleteButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith] * 0.65, ItemHeight * 100) text:@"确认删除 (0)" textColor:[UIColor whiteColor] imageName:@"" font:18];
        _ensureDeleteButton.backgroundColor = [UIColor themeColor];
        [_ensureDeleteButton addTarget:self action:@selector(option_ensureDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ensureDeleteButton;
}


-(UIButton *)allSelectButton
{
    if (!_allSelectButton) {
        _allSelectButton = [ToolClass createButtonWithFrame:CGRectMake([UIScreen zj_screenWith] * 0.65, 0, [UIScreen zj_screenWith] * 0.35, ItemHeight * 100) text:@"全选" textColor:[UIColor textBlackColor] imageName:@"" font:18];
        _allSelectButton.backgroundColor = [UIColor whiteColor];
        _allSelectButton.layer.borderColor = [UIColor themeColor].CGColor;
        _allSelectButton.layer.borderWidth = normalBorderWidth;
        CGFloat titleWidth = [@"全选" boundingRectWithSize:CGSizeMake([UIScreen zj_screenWith], CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(18)} context:nil].size.width + ItemWidth * 30;
        [_allSelectButton setImage:[UIImage imageNamed:@"list_select"] forState:UIControlStateNormal];
        [_allSelectButton setImage:[UIImage imageNamed:@"list_selected"] forState:UIControlStateSelected];
        [_allSelectButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -ItemWidth * 72, 0, ItemWidth * 72)];
        [_allSelectButton setImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth, 0, -titleWidth)];
        [_allSelectButton addTarget:self action:@selector(option_allSelectButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _allSelectButton;
}


-(UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [ToolClass createButtonWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - ItemHeight * 100, [UIScreen zj_screenWith], ItemHeight * 100) text:@"添加其他人员信息" textColor:[UIColor whiteColor] imageName:@"add_people" font:18 imageSize:ItemHeight * 35];
        _addButton.backgroundColor = [UIColor themeColor];
        [_addButton addTarget:self action:@selector(option_addButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(NSMutableArray<YBSignInPeopleInfoModel *> *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


-(NSMutableArray<YBSignInPeopleInfoModel *> *)currentDeleteArray
{
    if (!_currentDeleteArray) {
        _currentDeleteArray = [NSMutableArray array];
    }
    return _currentDeleteArray;
}


@end
